package com.hzcx.app.simplechat.model;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;

import com.hzcx.app.simplechat.api.APIRequest;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.api.RxHelper;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.bean.UpLoadImgBean;
import com.hzcx.app.simplechat.ui.login.bean.DomainNameBean;
import com.hzcx.app.simplechat.ui.main.MainActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.ui.publicui.bean.PayListBean;
import com.hzcx.app.simplechat.ui.publicui.bean.ProtocolBean;
import com.hzcx.app.simplechat.ui.publicui.bean.ShareInfoBean;
import com.hzcx.app.simplechat.ui.publicui.bean.ShareMineInfoBean;
import com.hzcx.app.simplechat.ui.publicui.bean.SloganMemberBean;
import com.hzcx.app.simplechat.ui.publicui.bean.UpdateVersionBean;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.file.FileUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.util.system.IMEI;
import com.hzcx.app.simplechat.util.token.TokenUtils;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 通用的相关
 */
public class PublicModel {

    /**
     * 上传图片
     *
     * @param context
     * @param token
     * @param url
     * @param observer
     */
    public static void upLoadImg(Activity context, String token, String url, BaseObserver<UpLoadImgBean> observer) {
        try {
            if (EmptyUtils.isEmpty(url)) {
                LogUtils.d("路径为空");
                return;
            }
            File file = new File(url);
            if (file.exists()) {
                url = file.getPath();
            } else {
                url = FileUtils.getImageAbsolutePath(context, Uri.parse(url));
            }
            File fileUpload = new File(url);
            LogUtils.d("路径是否存在:" + fileUpload.exists() + "  filepath:" + fileUpload.getPath());
            RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), fileUpload);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("file", fileUpload.getName(), requestFile);
            APIRequest
                    .getRequestInterface()
                    .upLoadImg(token, body)
                    .compose(RxHelper.observableIO2Main(context))
                    .subscribe(observer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void upLoadVideo(Activity context, String token, String url, BaseObserver<UpLoadImgBean> observer) {
        try {
            if (EmptyUtils.isEmpty(url)) {
                LogUtils.d("路径为空");
                return;
            }
            File file = new File(url);
            if (file.exists()) {
                url = file.getPath();
            } else {
                url = FileUtils.getImageAbsolutePath(context, Uri.parse(url));
            }
            File fileUpload = new File(url);
            LogUtils.d("路径是否存在:" + fileUpload.exists() + "  filepath:" + fileUpload.getPath());
            RequestBody requestFile = RequestBody.create(MediaType.parse("video/MP4"), fileUpload);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("file", fileUpload.getName(), requestFile);
            APIRequest
                    .getRequestInterface()
                    .upLoadImg(token, body)
                    .compose(RxHelper.observableIO2Main(context))
                    .subscribe(observer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传图片
     *
     * @param token
     * @param url
     * @param observer
     */
    public static void upLoadChatImg(String token, String url, BaseObserver<UpLoadImgBean> observer) {
        try {
            if (EmptyUtils.isEmpty(url)) {
                LogUtils.d("路径为空");
                return;
            }
            File fileUpload = new File(url);
            LogUtils.d("路径是否存在:" + fileUpload.exists() + "  filepath:" + fileUpload.getPath());
            RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), fileUpload);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("file", fileUpload.getName(), requestFile);
            APIRequest
                    .getRequestInterface()
                    .upLoadImg(token, body)
                    .compose(RxHelper.observableIO2Main(MainActivity.getInstance().getActivity()))
                    .subscribe(observer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     *
     * @param context
     * @param url
     * @param observer
     */
    public static void upLoadFile(Context context, String url, BaseObserver<UpLoadImgBean> observer) {
        try {
            File file = new File(url);
            LogUtils.d("路径是否存在:" + file.exists());
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("file", file.getName(), requestFile);
            APIRequest
                    .getRequestInterface()
                    .upLoadImg(TokenUtils.getTokenHeader(), body)
                    .compose(RxHelper.observableIO2Main(context))
                    .subscribe(observer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送验证码
     *
     * @param context
     * @param mobile
     * @param event
     */
    public static void sendCode(Context context, String mobile, String event, String area_code, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .sendCode(mobile, event, area_code, AppHelper.getDeviceBrand(), IMEI.getDeviceIMEI())
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 验证验证码是否正确
     *
     * @param context
     * @param mobile
     * @param captcha
     * @param event
     *
     * @param observer
     */
    public static void checkCode(Context context, String mobile, String captcha, String event, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .checkCode(mobile, captcha, event)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 根据类型获取协议内容
     *
     * @param context
     * @param type
     * @param observer
     */
    public static void getProtocolByType(Context context, String type, BaseObserver<ProtocolBean> observer) {
        APIRequest
                .getRequestInterface()
                .getProtocolByType(TokenUtils.getTokenHeader(), type)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 根据环信名称获取用户信息
     *
     * @param context
     * @param hxusername
     * @param observer
     */
    public static void getUserInfoByHx(Context context, String hxusername, BaseObserver<HxUserInfoBean> observer) {
        APIRequest
                .getRequestInterface()
                .getUserInfoByHx(TokenUtils.getTokenHeader(), hxusername)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 获取分享内容
     *
     * @param context
     * @param member_id
     * @param observer
     */
    public static void getShareInfo(Context context, int member_id, BaseObserver<ShareInfoBean> observer) {
        APIRequest
                .getRequestInterface()
                .getShareInfo(TokenUtils.getTokenHeader(), member_id)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 根据口令获取数据
     *
     * @param context
     * @param slogan
     * @param observer
     */
    public static void getSloganMenber(Context context, String slogan, BaseObserver<SloganMemberBean> observer) {
        APIRequest
                .getRequestInterface()
                .getSloganMenber(TokenUtils.getTokenHeader(), slogan)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 邀请好友分享内容
     *
     * @param context
     * @param observer
     */
    public static void getShareMineInfo(Context context, BaseObserver<ShareMineInfoBean> observer) {
        APIRequest
                .getRequestInterface()
                .getShareMineInfo(TokenUtils.getTokenHeader())
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 上传截图图片
     *
     * @param context
     * @param imgUrl
     * @param observer
     */
    public static void riskRecordImg(Context context, String imgUrl, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .riskRecordImg(TokenUtils.getTokenHeader(), imgUrl)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 获取版本更新内容
     *
     * @param context
     * @param type
     * @param version_number
     * @param observer
     */
    public static void updateVersion(Context context, String type, String version_number, BaseObserver<UpdateVersionBean> observer) {
        APIRequest
                .getRequestInterface()
                .updateVersion(TokenUtils.getTokenHeader(), type, version_number)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 应用到前台-通知服务端
     *
     * @param context
     * @param observer
     */
    public static void putAppOnlineRecord(Context context, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .putAppOnlineRecord(TokenUtils.getTokenHeader())
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 通知服务器在线
     *
     * @param context
     * @param observer
     */
    public static void putServiceOnline(Context context, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .putServiceOnline(TokenUtils.getTokenHeader(),AppHelper.getVersionName(context))
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 获取支付价格列表
     *
     * @param context
     * @param observer
     */
    public static void getPayList(Context context, BaseObserver<List<PayListBean>> observer) {
        APIRequest
                .getRequestInterface()
                .getPayList(TokenUtils.getTokenHeader())
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 上传用户地址
     *
     * @param context
     * @param lat
     * @param lng
     * @param address
     * @param observer
     */
    public static void putUserLocation(Context context, double lat, double lng, String address, BaseObserver<BaseEmptyBean> observer) {
        APIRequest
                .getRequestInterface()
                .putUserLocation(TokenUtils.getTokenHeader(), lat, lng, address)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     *  获取域名配置
     * @param context
     * @param url
     * @param type
     * @param observer
     */
    public static void getDomainNameConfig(Context context, String url, int type, BaseObserver<DomainNameBean> observer){
        APIRequest
                .getRequestInterface()
                .getDomainNameConfig(url,type)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }
}
